<?php
require_once '../../Connections/level2/L2connect.php';
require_once('../../class/zdisplayFunction.php');
//$log = new KLogger ( "../../log/log.txt" ,KLogger::INFO );
$queryExecutor = new QueryExecutor();
$totalSales=0.00;
$dateBegin = "";
$dateBeginStatus = FALSE;
$dateBeginArr = array();
$dateBeginSQL = "";

$dateEnd = "";
$dateEndStatus = FALSE;
$dateEndArr = array();
$dateEndSQL = "";

if(isset($_POST['begin'])){
    $dateBegin = trim($_POST['begin']);
    $dateBeginStatus = TRUE;
    $dateBeginArr = explode("/",$dateBegin);
    $dateBeginSQL = ($dateBeginArr[2]-543)."-".$dateBeginArr[1]."-".$dateBeginArr[0]." 00:00:00";
}
if(isset($_POST['end'])){
    $dateEnd = trim($_POST['end']);
    $dateEndStatus = TRUE;
    $dateEndArr = explode("/",$dateEnd);
    $dateEndSQL = ($dateEndArr[2]-543)."-".$dateEndArr[1]."-".$dateEndArr[0]." 23:59:59";
}

if( $dateEndStatus && $dateBeginStatus ){
    $sqltp="SELECT sum(totalprice) FROM sme_order WHERE status=4 AND orderactiondate BETWEEN '$dateBeginSQL' AND '$dateEndSQL' GROUP BY status";
    $totalSalesStr=$queryExecutor->queryForString($sqltp); 
    $totalSales= $totalSalesStr ? $totalSalesStr : 0.00;
}
else if($dateBeginStatus)
{
    $dateBeginSQL2 = ($dateBeginArr[2]-543)."-".$dateBeginArr[1]."-".$dateBeginArr[0]." 23:59:59";
    $sqltp="SELECT sum(totalprice) FROM sme_order WHERE `status`=4 AND orderactiondate BETWEEN '$dateBeginSQL' AND '$dateBeginSQL2' GROUP BY `status`";
    $totalSalesStr = $queryExecutor->queryForString($sqltp); 
    $totalSales= $totalSalesStr ? $totalSalesStr : 0.00;

}

$json_data[]=array(
    "dateBegin"=>$dateBegin,
    "dateEnd"=>$dateEnd,
    "totalSales"=>format_number(round($totalSales,2))
); 
$json = json_encode($json_data);
$queryExecutor->close();  
echo $json ;